package com.hechao.controller;

import cn.hutool.core.util.RandomUtil;
import com.hechao.code.Code;
import com.hechao.domain.EmailMessage;
import com.hechao.result.Result;
import com.hechao.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


@RestController
@CrossOrigin
public class MessageController {
    @Autowired
    MessageService messageService;

    //*不用redis可以删去该自动装配
    @Autowired
    RedisTemplate<String, String> redisTemplate;


    //邮箱验证码登录
    @GetMapping("sendEmail/{getPeople}")
    public Result sendSimpleMail(@PathVariable("getPeople") String getPeople) {

        System.out.println(getPeople);

        //从Redis中获取code
        String RandomMath = redisTemplate.opsForValue().get(getPeople);
        System.out.println("redis根据输入的邮箱查询库中是否存在验证码，返回值:" + RandomMath);
        if (!StringUtils.isEmpty(RandomMath)) {  //验证码还未过期，在库中，可以直接登录
            return new Result("验证码还未过期，请输入", Code.EMAIL_SEND_NONO, RandomMath);
        }

        //验证码过期或者第一次登录生成验证码
        RandomMath = RandomUtil.randomNumbers(6);
        System.out.println("随机验证码为" + RandomMath);
        // 构建一个邮件对象
        Boolean mail = messageService.sendMail(RandomMath, getPeople);
        if (mail) {
            //设置有效时间,这里是30秒，
            redisTemplate.opsForValue().set(getPeople, RandomMath, 30, TimeUnit.SECONDS);
            return new Result("验证码发送成功，30秒之后失效", Code.EMAIL_SEND_OK, RandomMath);
        }
        return new Result("验证码发送失败", Code.EMAIL_SEND_ERR, RandomMath);
    }

    @PostMapping("/loginE")
    public Result loginE(@RequestBody EmailMessage emailMessage) {
        System.out.println("前端传递的邮箱号:" + emailMessage.getEmail());
        System.out.println("前端传递的验证码:" + emailMessage.getMath());
        //查询redis数据库中根据邮箱号查询验证码
        String s = redisTemplate.opsForValue().get(emailMessage.getEmail());
        if (emailMessage.getMath().equals(s)) {   //输入的验证码和redis数据库中生成的验证码相同时
            return new Result("登录成功", Code.EMAIL_LOGIN_OK, s);
        }
        return new Result("验证码不一致", Code.EMAIL_LOGIN_ERR, null);
    }


}
